#include "widget.h"
#include "ui_widget.h"

#include <QDebug>
#include <QMenu>
#include <QMap>
#include <QVariant>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    if (ui) {
        delete ui;
        ui = nullptr;
    }
}

void Widget::on_btnIniItems1_clicked()
{
    QIcon icon;
    icon.addFile(":/res/aim.ico");

    QList<QString> strList;
    strList.append("葡萄");
    strList.append("凤梨");
    strList.append("榴莲");
    strList.append("西瓜");
    strList.append("柑柚");

    ui->comboBox1->clear();
    foreach (const QString &str, strList) {
        ui->comboBox1->addItem(icon, str);
    }
}

void Widget::on_btnClearItems_clicked()
{
    ui->comboBox1->clear();
}

void Widget::on_chkBoxEditable_clicked(bool checked)
{
    ui->comboBox1->setEditable(checked);
}

void Widget::on_comboBox1_currentTextChanged(const QString &arg1)
{
    ui->plainTextEdit->appendPlainText(arg1);
}

void Widget::on_btnIniItems2_clicked()
{
    QMap<QString, int> City_Zone;
    City_Zone.insert("北京",10);
    City_Zone.insert("上海",21);
    City_Zone.insert("天津",22);
    City_Zone.insert("大连",411);
    City_Zone.insert("锦州",416);
    City_Zone.insert("徐州",516);
    City_Zone.insert("福州",591);
    City_Zone.insert("青岛",532);

    ui->comboBox2->clear();
    foreach (const QString &str, City_Zone.keys()) {
        ui->comboBox2->addItem(str, City_Zone.value(str));
    }
}

void Widget::on_comboBox2_currentIndexChanged(int index)
{
    Q_UNUSED(index);

    QString city = ui->comboBox2->currentText();
    QString zone = ui->comboBox2->currentData().toString();
    ui->plainTextEdit->appendPlainText(city + " : 区号 = " + zone);
}

void Widget::on_btnClearText_clicked()
{
    ui->plainTextEdit->clear();
}

void Widget::on_plainTextEdit_customContextMenuRequested(const QPoint &pos)
{
    QMenu *menu = ui->plainTextEdit->createStandardContextMenu();
    menu->exec(pos);
}

